/**************************************************************************
 * @doc CORE
 * @module Funcs.C |
 * Source code for handling the Function table Flexi record.
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :
 * <nl>Notes       :
 *
 * @head3 Core Functions |
 * @index | Core
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 *
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form
 * or by any means whether electronic, mechanical, magnetic, optical,
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log:  $
 * ------------------------------------------------------------------------
 ********************************************************************************/

#include <project.h>
#define VSFUNC_C

#include <stdlib.h>
#include <applidl.h>
#include <aclconio.h>
#include <string.h>

/** /#define DEBUG_VSFUNC*/
#ifdef DEBUG_VSFUNC
#define DEBUG_95
#endif

#include <define.h>
#include <table.h>
#include <transact.h>
#include <funcs.h>
#include <gds.h>
#include <hdt.h>
#include <trt.h>
#include <batch.h>
#include <fields.h>
#include <tip.h>
#include <spdebug.h>
#include <msgfuncs.h>
#include <codesec.h>

/*********************************************************************************************
* @func int | inRunEngine |
*       Calls the sequence on functions in the TRT and OPT files
*
* @parm short * |pshFuncPtr |
*       Array of functions
*
* @parm void * | pvdData |
*       The data Object required for the function
*
* @rdesc return values.
* @flag int    |  VS_ERR or VS_SUCCESS
* @end
**********************************************************************************************/

int inRunEngine(short* pshFuncPtr,TRANSACTION_OBJECT* pvdData) {
    signed int inResult = VS_SUCCESS;
    unsigned short shCode;
    int inCount = 0;
    PFI_VD pinFunction;

    pdebug(("--inRunEngine--"));


    shCode = pshFuncPtr[inCount];
    /*
     * 3200 default is scrolling, but
     * a host can override this in its
     * flow; however, we reset it back
     * to the default after every flow,
     * regardless of what the host did.
     */
    inScrollOn(pvdData);

    while (shCode) {

        if ((pinFunction = (PFI_VD) inGetFunctionPointer(shCode)) == NULL)
            return(VS_ERR);
        if (pinFunction != (PFI_VD) 1)  // means NO_ACCESS
        {

            pdebug(("shCode (%x)", shCode));
            if ((inResult = (pinFunction) (pvdData)) != VS_SUCCESS) {
                inScrollOn(pvdData);
                if (lnGGErrorMessage() != 0L) {
                    char szRespMsg[RESP_MSG_SIZE + 1];

                    vdGetMessageFromFile(lnGGErrorMessage(), szRespMsg);
                    //BT_TRACE ('H', szRespMsg);
                    vdDisplayErrorMessage(lnGGErrorMessage());
                }
                return(inResult);
            }
        }
        shCode = pshFuncPtr[++inCount];
    }

    inScrollOn(pvdData);

    return(inResult);
}

/*********************************************************************************************
* @func int | inRunTransProtocolFunction |
*       Uses the Protocol ID from the HDT table to retrieve the protocol
*       function from the function table flexi record and runs the function
*
* @parm PACKET_OBJECT * |pobPacket |
*       Packet object
*
* @end
**********************************************************************************************/


int inRunTransProtocolFunction(TRANSACTION_OBJECT* pobTran) {
    PFI_TO pinFunction;

    if ((pinFunction = (PFI_TO) inGetFunctionPointer(shGetTransProtocol())) == NULL)
        return(VS_ERR);

    return((pinFunction) (pobTran));
}

/*********************************************************************************************
* @func int | inRunSettleProtocolFunction |
*       Uses the Protocol ID from the HDT table to retrieve the protocol
*       function from the function table flexi record and runs the function
*
* @parm PACKET_OBJECT * |pobPacket |
*       Packet object
*
* @end
**********************************************************************************************/

int inRunSettleProtocolFunction(TRANSACTION_OBJECT* pobTran) {
    PFI_TO pinFunction;

    if ((pinFunction = (PFI_TO) inGetFunctionPointer(shGetSettleProtocol())) == NULL)
        return(VS_ERR);

    return((pinFunction) (pobTran));
}


int FUNCS_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}


#undef VSFUNC_C

